package com.adjy.pathbuilders;

import java.io.IOException;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;

import com.adjy.pathbuilders.commons.FontLoader;
import com.adjy.pathbuilders.database.GameDatabase;
import com.adjy.pathbuilders.rendering.DisplayConverter;
import com.adjy.pathbuilders.rendering.RenderingSurface;
import com.adjy.pathbuilders.screens.content.CustomDialog;
import com.adjy.pathbuilders.sounds.SoundManager;
import com.adjy.pathbuilders.state.Game;
import com.facebook.Request;
import com.facebook.Request.Callback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class MainActivity extends Activity {

	private Game game;
	private RenderingSurface surface;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		CustomDialog.getInstance().setContext(this);
		surface = (RenderingSurface) findViewById(R.id.rendering_surface);
		
		// Set Volume to our Stream
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		// Set Display Settings - Must be before game
		setDisplaySettings();
		GameDatabase.createDatabase(getApplicationContext());
		SoundManager.create(getApplicationContext());
		FontLoader.create(getApplicationContext());
		try {
			game = new Game(getApplicationContext(), this);
			game.load();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1); // Exits
		}
		surface.setGame(game);
		
	}
	
	@Override
	protected void onResume() {
		game.resume();
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		game.pause();
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	@SuppressWarnings("deprecation")
	private void setDisplaySettings() {
		WindowManager w = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			Point size = new Point();
	        w.getDefaultDisplay().getSize(size);
	        DisplayConverter.setScreenLengths(size.x, size.y); 
	    } else {
	        Display d = w.getDefaultDisplay(); 
	        DisplayConverter.setScreenLengths(d.getWidth(), d.getHeight());
	    }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
		if (game != null)
			game.pause();
	}
	
	public void fbPostPhoto(Bitmap photo, String description){
		if(Session.getActiveSession() == null || Session.getActiveSession().isClosed()) return;
		if(photo == null) System.out.println("Photo is null!");
		Request photoRequest = Request.newUploadPhotoRequest(Session.getActiveSession(), photo, new Callback() {
			@Override
			public void onCompleted(Response response) {
				if(!Session.getActiveSession().getState().isOpened()) return;
				if(!Session.getActiveSession().isOpened()) return;
				if(response.getError() != null){
					System.out.println("Did not post... ):");
					Log.e("FRAGACTIVITY", response.getError().toString());
				}else
					System.out.println("The photo was posted! \\o/");
			}
		});
		Bundle params = photoRequest.getParameters();
		params.putString("message", description);
		photoRequest.executeAsync();
	}
	
	@Override
	  public void onActivityResult(int requestCode, int resultCode, Intent data) {
	      super.onActivityResult(requestCode, resultCode, data);
	      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	  }

}
